package com.chestnut.contentcore.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chestnut.common.log.annotation.Log;
import com.chestnut.common.log.enums.BusinessType;
import com.chestnut.common.security.web.PageRequest;
import com.chestnut.common.utils.IdUtils;
import com.chestnut.contentcore.core.IContentType;
import com.chestnut.contentcore.domain.*;
import com.chestnut.contentcore.domain.vo.ContentVO;
import com.chestnut.contentcore.listener.event.AfterContentEditorInitEvent;
import com.chestnut.contentcore.mapper.CmsPageRichMapper;
import com.chestnut.contentcore.properties.ShortTitleLabelProperty;
import com.chestnut.contentcore.properties.SubTitleLabelProperty;
import com.chestnut.contentcore.service.ICmsPageRichService;
import com.chestnut.contentcore.user.preference.ShowContentSubTitlePreference;
import com.chestnut.contentcore.util.ContentCoreUtils;
import com.chestnut.system.security.StpAdminUtil;
import com.chestnut.system.validator.LongId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.chestnut.common.security.web.BaseRestController;

import com.chestnut.common.security.anno.Priv;
import com.chestnut.system.security.AdminUserType;
import lombok.RequiredArgsConstructor;
import com.chestnut.common.domain.R;

/**
 * 单页富文本呢Controller
 *
 * @author chestnut
 * @date 2025-04-28
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/rich")
public class CmsPageRichController extends BaseRestController {

    @Autowired
    private ICmsPageRichService cmsPageRichService;
    @Autowired
    private CmsPageRichMapper cmsPageRichMapper;

    /**
     * 查询单页富文本呢列表
     */

    @GetMapping("/list")
    public R<?> list(CmsPageRich cmsPageRich)
    {
        PageRequest pr = this.getPageRequest();
        LambdaQueryChainWrapper<CmsPageRich> wrapper = this.cmsPageRichService.lambdaQuery();
                    if (cmsPageRich.getName() != null) {
                        wrapper.like(CmsPageRich::getName,cmsPageRich.getName());
                    }
        if (cmsPageRich.getLanguage() != null) {
            wrapper.like(CmsPageRich::getLanguage,cmsPageRich.getLanguage());
        }
        wrapper.orderByAsc(CmsPageRich::getSort);
        Page<CmsPageRich> page = wrapper
                .page(new Page<>(pr.getPageNumber(), pr.getPageSize(), true));

        return this.bindDataTable(page);
    }

    /**
     * 获取单页富文本呢详细信息
     */

    @GetMapping(value = "/{id}")
    public R<?> getInfo(@PathVariable("id") Long id)
    {
        return R.ok(this.cmsPageRichService.getById(id));
    }



    @GetMapping("/getByLauType/{language}/{type}")
    public R<?> initContentEditor(@PathVariable("language")  String language,
                                          @PathVariable("type") String type) {
        QueryWrapper<CmsPageRich> wrapper = new QueryWrapper<CmsPageRich>();
        wrapper.eq("language",language);
        wrapper.eq("type",type);
        wrapper.orderByDesc("create_time");
       List<CmsPageRich> cmsPageRichList= cmsPageRichMapper.selectList(wrapper);
       if (!cmsPageRichList.isEmpty()) {
           return R.ok(cmsPageRichList.get(0));
       }else{
           return R.ok(null);
       }
    }



    /**
     * 新增单页富文本呢
     */

    @Log(title = "单页富文本呢", businessType = BusinessType.INSERT)
    @PostMapping
    public R<?> add(@RequestBody CmsPageRich cmsPageRich)
    {
        cmsPageRich.setCreateBy(StpAdminUtil.getLoginUser().getUsername());
        cmsPageRich.setCreateTime(LocalDateTime.now());
        this.cmsPageRichService.save(cmsPageRich);
        return R.ok();
    }

    /**
     * 修改单页富文本呢
     */

    @Log(title = "单页富文本呢", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<?> edit(@RequestBody CmsPageRich cmsPageRich)
    {
        this.cmsPageRichService.updateById(cmsPageRich);
        return R.ok();
    }

    /**
     * 删除单页富文本呢
     */

    @Log(title = "单页富文本呢", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public R<?> remove(@PathVariable Long[] ids)
    {
        this.cmsPageRichService.removeBatchByIds(List.of(ids));
        return R.ok();
    }
}
